﻿// ===============================================================================
// Project Name        :    ResourcesFile4DotNet.GetResource
// Project Description :   
// ===============================================================================
// Class Name          :    CGetImage
// Class Version       :    v1.0.0.1
// Class Description   :    从资源文件中获取图片资源
// Author              :    HUANGKX 小飞兔
// Create Time         :    2018/1/18 16:33:02
// Update Time         :    2018/1/19 9:40:15
// ===============================================================================
// Copyright 2018 HUANGKX 小飞兔 . 
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ===============================================================================

using System.Collections;
using System.Drawing;
using System.Resources;

namespace ResourcesFile4DotNet.GetResource
{
    /// <summary>
    /// 从资源文件中获取图片资源
    /// </summary>
    class CGetImage
    {
        /// <summary>
        /// 默认资源文件名，只需指定图片资源名称（资源文件在程序同目录下）
        /// </summary>
        /// <param name="sImageName">图片资源名称</param>
        /// <returns>图片，如果返回null表示未在资源文件中找到指定资源名称的资源</returns>
        public static Image GetImageFromRescoure(string sImageName)
        {
            string str = System.AppDomain.CurrentDomain.BaseDirectory;
            // ResourcesFile4DotNet.Resources 是默认资源文件名
            str = str.Substring(0, str.LastIndexOf("\\")) + "\\ResourcesFile4DotNet.resources";
            if (!System.IO.File.Exists(str))
                return null;

            return GetImageByPathFromRescoure(str, sImageName);
        }

        /// <summary>
        /// 指定资源文件名称，指定图片资源名称，获取图片资源（资源文件在程序同目录下）
        /// </summary>
        /// <param name="sResourceName">资源文件名称</param>
        /// <param name="sImageName">资源名称</param>
        /// <returns>图片，如果返回null表示未在资源文件中找到指定资源名称的资源</returns>
        public static Image GetImageFromRescoure(string sResourceName, string sImageName)
        {
            string str = System.AppDomain.CurrentDomain.BaseDirectory;
            str = str.Substring(0, str.LastIndexOf("\\"));
            if (!System.IO.File.Exists(str + "\\" + sResourceName))
                return null;

            return GetImageByPathFromRescoure(str + "\\" + sResourceName, sImageName);
        }

        /// <summary>
        /// 指定资源文件地址及具体图片名称，获取图片（资源文件在任意目录下）
        /// </summary>
        /// <param name="sResPath">资源文件完整路径</param>
        /// <param name="sImageName">资源名称</param>
        /// <returns>图片，如果返回null表示未在资源文件中找到指定资源名称的资源</returns>
        public static Image GetImageByPathFromRescoure(string sResPath, string sImageName)
        {
            Image image = null;
            IResourceReader reader = new ResourceReader(sResPath);
            IDictionaryEnumerator en = reader.GetEnumerator();

            while (en.MoveNext())
            {
                if (en.Key.ToString() == sImageName)
                    image = en.Value as Image;
            }
            return image;
        }
    }
}
